#include "CocosPCH.h" 

#include "CoCMovieClipNode.h"

// implementation of CoCMovieClipNode

CoCMovieClipNode::CoCMovieClipNode()
{
}

CoCMovieClipNode::~CoCMovieClipNode()
{
	m_vecShapNode.clear();
}

CoCMovieClipNode* CoCMovieClipNode::create(char cFrameRate, unsigned short nFrameCount)
{
    CoCMovieClipNode* pRet = new CoCMovieClipNode();
    if (pRet && pRet->initWithData(cFrameRate, nFrameCount))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }
    
    return pRet;
}

bool CoCMovieClipNode::initWithData(char nFrameRate, unsigned short nFrameCount)
{    
	//CCSprite::initWithTexture(pTexture);
	// shader program
	m_pShapeRoot = CCNode::create();
	addChild(m_pShapeRoot);

	m_nFrameCount = nFrameCount;
	m_cFrameRate = nFrameRate;
	m_vecFrameInfo.resize(nFrameCount);

	m_nCurrentFrame = 0;

	schedule(schedule_selector(CoCMovieClipNode::updateFrame), 1.0f / nFrameRate);

    return true;
}

void CoCMovieClipNode::AddShapdeNode(CoCShapeNode *pNode)
{
	if(pNode)
		m_pShapeRoot->addChild(pNode);
	m_vecShapNode.push_back(pNode);
    Rect rc = getRect();
    setContentSize(rc.size);
    //setAnchorPoint(Vec2(-rc.origin.x / rc.size.width, -rc.origin.y / rc.size.height));
    Vec2 pos = rc.origin * -1.0f;
    pos.x -= rc.size.width*0.5f;
    pos.y -= rc.size.height*0.5f;
    m_pShapeRoot->setPosition(pos);
}

void CoCMovieClipNode::AddFrameInfo(int nFrameIndex, FrameInfo *pInfo)
{
	m_vecFrameInfo[nFrameIndex].push_back(pInfo);
}

void CoCMovieClipNode::updateFrame(float ft)
{
	if(m_nCurrentFrame >= m_nFrameCount)
		m_nCurrentFrame = 0;

	for(int i = 0; i < m_vecShapNode.size(); i++)
	{
		if(m_vecShapNode[i])
			m_vecShapNode[i]->setVisible(false);
	}

	if(m_nCurrentFrame >= m_vecFrameInfo.size())
		return ;
	
	std::vector<FrameInfo*> &FrameInfo = m_vecFrameInfo[m_nCurrentFrame];
	for(int i = 0; i < FrameInfo.size(); i++)
	{
		CoCShapeNode *pShapeNode = m_vecShapNode[FrameInfo[i]->nShapeIndex];
		if(pShapeNode)
		{
			pShapeNode->setVisible(true);
			if(FrameInfo[i]->trans != NULL)
				pShapeNode->setAdditionalTransform(*FrameInfo[i]->trans);
			else
				pShapeNode->setAdditionalTransform(AffineTransform::IDENTITY);

			if(FrameInfo[i]->color != NULL)
				pShapeNode->setColor(*FrameInfo[i]->color);
			else
				pShapeNode->setColor(Color4B(255, 255, 255, 255));
		}
		else
		{
			pShapeNode = pShapeNode;
		}
	}
	m_nCurrentFrame++;
}

Rect CoCMovieClipNode::getRect()
{
	float fMinX, fMinY, fMaxX, fMaxY;
	Rect rc = Rect::ZERO;
	bool bFirst = true;
	for(int i = 0; i < m_vecShapNode.size(); i++)
	{
		if(m_vecShapNode[i])
		{
			if(bFirst)
			{
				bFirst = false;
				rc = m_vecShapNode[i]->getRect();
				fMinX = rc.origin.x;
				fMaxX = rc.origin.x + rc.size.width;
				fMinY = rc.origin.y;
				fMaxY = rc.origin.y + rc.size.height;
			}
			else
			{
				rc = m_vecShapNode[i]->getRect();
				fMinX = rc.origin.x < fMinX ? rc.origin.x : fMinX;
				fMaxX = rc.origin.x+rc.size.width > fMaxX ? rc.origin.x+rc.size.width : fMaxX;
				fMinY = rc.origin.y < fMinY ? rc.origin.y : fMinY;
				fMaxY = rc.origin.y+rc.size.height > fMaxY ? rc.origin.y+rc.size.height : fMaxY;
			}
		}
	}
	if(bFirst)
	{
		return Rect::ZERO;
	}
	rc.origin.x = fMinX;
	rc.origin.y = fMinY;
	rc.size.width = fMaxX-fMinX;
	rc.size.height = fMaxY-fMinY;
	return rc;
}